# *_*coding:utf-8 *_*
import requests
from loguru import logger
from tools.handle_path import api_base_dir


class HandleRequest:
    def __init__(self):
        # 初始化Session类
        self.s = requests.Session()
        # 添加请求头
        self.s.headers = {"Content-Type": "application/json", "X-Lemonban-Media-Type": "lemonban.v2"}

    # 发送请求
    def send_request(self, method, url, data=None, token=None, **kwargs):
        self.__headers(token)
        logger.info(f"请求头信息为:{self.s.headers}")
        url = self.__url(url)
        logger.info(f"请求方法为:{method}")
        logger.info(f"请求数据为:{data},数据类型为:{type(data)}")
        if method.upper() == "GET":
            request_result = self.s.get(url, params=eval(data), **kwargs)

        else:
            request_result = self.s.request(method, url, data=data.encode("utf-8"), **kwargs)
        logger.info(f"响应结果为:{request_result}")
        logger.info(f"响应结果json数据为{request_result.json()}")
        return request_result

    def __url(self, url):
        url = api_base_dir + url
        logger.info(f'请求地址为：{url}')
        return url

    def __headers(self, token=None):
        if token:
            self.s.headers.update({"Authorization": token})


if __name__ == '__main__':
    method = "get"
    url = "verificationCode/message"
    params = '{"phone": 13817694710}'
    hr = HandleRequest()
    res = hr.send_request(method, url, data=params)
    print(res.json())
